<?php
/**
 * @author Feng
 * @date 2012-7-6
 */
class controller_admin_resource_keyword extends system_user_controller_panel {
	
	private $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		$this->concept = new resource_keyword_concept($this->user->getLang());
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Resource'))->setRoute('resource/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Keywords'))->setRoute('resource/keyword/index'));
	}
		
	public function action_index() {
		$this->_goRoute('resource/keyword/list');
		$this->render();
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$this->view->row = $row;
		$keywords = $this->_post('keywords');
		$row->setIsActive(1);
		if($this->getRequest()->isPost() && $keywords) {
			$keywords = preg_replace('/[\r\n,]+/isU', ';', $keywords);
			$_kw = array();
			foreach(explode(';', $keywords) AS $k) {
				$_k = trim($k);
				if($_k == '') continue;
				$_kw[] = $_k;
			}
			if(!empty($_kw)) {
				$active = intval($this->_post('is_active')) ? 1 : 0;
				foreach($_kw AS $key => $kw) {
					if($this->concept->fetch(array('name = ?' => $kw))) {
						unset($_kw[$key]);
						continue;
					}
					try {
						system_db::getAdapter()->beginTransaction();
						$_row = $this->concept->create();
						$_row->setName($kw);
						$_row->setIsActive($active);
						$_row->save();
						unset($_kw[$key]);
						system_db::getAdapter()->commit();
					} catch (Exception $e) {
						system_db::getAdapter()->rollBack();
						$this->view->throwException = $e;
					}				
				}
			}
			if(empty($_kw)) {
				$this->referer = $this->_post('referer');
				$this->view->throwMessage = 'Complete';
			}
			$keywords = implode(';', $_kw);
		} else {
			$row = $this->_saveRow($row);
		}
		$this->view->keywords = $keywords;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Keyword'))->setRoute('resource/keyword/addnew'));
		$this->render('resource/keyword/row.phtml');
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('id = ?' => $id)))) {
			throw new collection_exception("The Keyword is not Existing.");
		}
		$row = $this->_saveRow($row);
		$concept = new picture_special_concept($this->user->getLang());
		$this->view->specialList = $concept->fetchAll(array("special.id IN(SELECT `special_id` FROM `picture_special_keyword` WHERE `keyword_id` = ?)" => $id));
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getName())->setRoute('resource/keyword/edit')->setParams(array('id' => $row->getId())));
		$this->render('resource/keyword/row.phtml');
	}
	
	private function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->_post('referer');
		$row->setName(trim($this->_post('name')));
		$row->setIsActive(intval($this->_post('is_active')) ? 1 : 0);		
		$this->_saveToDatabase($row, 'logo', 'image');		
		return $row;
	}
	
	public function action_list() {
		$this->_batchDelete($this->concept, 'id', 'id');
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		
		$this->view->order = $order = strtolower($this->_get('order'));
		$keyword = trim($this->_get('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$adapter->where("`name` LIKE ?", "%$keyword%");
		}
		$_order = array("id DESC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'total': case 'day': case 'week': case 'month': {
					$order = 'hit_' . $order;
					break;
				}
				case 'active': {
					$order = 'is_active';
					break;
				}
				case 'ptotal': case 'stotal': {
					break;
				}
				default: {
					$sort = !$sort;
					$order = 'id';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$this->view->list = new system_paginator($adapter);
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/keyword/list'));
		$this->render();
	}
}
